<?php
/**
 *
 * 版权所有：忠赢网站管理系统<qwadmin.qiawei.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：顾问列表
 *
 **/

namespace Qwadmin\Controller;
use Qwadmin\Model\PhotosModel;

class PageController extends ComController
{
    public function index() {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        // $where['deleted'] = PhotosModel::STATUS_ON;
        $order = 'id desc';
        
        $photos = M('pages');
        $pagesize = 10;//每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $photos->where($where)->count();
        $pages = $photos->order($order)->limit($offset . ',' . $pagesize)->select();
        // $article = M("pages")->find($aid);
        //var_dump($pages);exit;
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('pages', $pages);
        $this->assign('info',$article);
        $this->assign('page', $page);
        $this->display();
    }

    public function add() {
        $content = M('pages')->select();
        $this->assign('pages',$pages);
        $this->display('form');
    }

    public function edit() {
        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        if (empty($id)) {
           $this->error('参数错误');
        }
        $pages=M("pages")->where(['id'=>$id])->find();
        $this->assign('pages',$pages);
        
        $this->display('form');
    }

    public function update() {
            $data['id'] = I('post.id', false, 'intval');
            $data['title']    =        I('post.title', '', 'htmlspecialchars');
            $data['content'] = html_entity_decode(isset($_POST['content']) ? $_POST['content'] : false);
            $data['keywords']    =      I('post.keywords', '', 'htmlspecialchars');
            $data['seotitle']     =     I('post.seotitle', '', 'htmlspecialchars');
            $data['description']  =        I('post.description', '', 'htmlspecialchars');
            $data['o']       =    I('post.o', '', 'htmlspecialchars');
            $data['dir']    =    I('post.dir', '', 'htmlspecialchars');
            $data['t']     = date('Y-m-d H:i:s');
        //var_dump($data);exit;
        $pages = M('pages');
        if (empty($data['id'])) { //增加
            if ($pages->add($data)) {
               $this->success('添加单页成功');
            } else {
                $this->error('添加单页失败');
            }
        } else { //编辑
            if ($pages->save($data)) {
                $this->success('编辑单页成功');
            } else {
                $this->error('编辑单页失败');
            }
        }

    }

    public function del() {

        $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
        if(is_array($uids)){
            $map['id'] = array('in', $ids);
        }else{
            $map['id'] = $ids;
        }
        if (M('pages')->where($map)->delete()) {
            addlog('删除单页ID：' . $ids);
            $this->success('单页删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }

}